/**
 * 保安报警常量管理
 */
package com.framework.web.restful.doamin;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.ServletRequest;
import java.util.Locale;

/**
 * @author huang.zhong
 * @since 2015-12-11
 * @version 1.00
 */
public class MessageReader {
	/**
	 * request
	 */
	private ServletRequest request;
	/**
	 * 构造方法
	 * @param request
	 */
	public MessageReader(ServletRequest request) {
		this.request = request;
	}
	
	/**
	 * 获取msg
	 * @param errorCode
	 * @return
	 */
	public String getMsg(Object errorCode) {
		return getMsg(errorCode, null);
	}

	/**
	 * 获取msg
	 * @param errorCode
	 * @return
	 */
	public String getMsg(Object errorCode, String[] args) {
		String code = StringUtils.EMPTY;
		if (errorCode instanceof String) {
			code = (String)errorCode;
		} else {
			code = String.valueOf(errorCode);
		}
		String errorMsg = StringUtils.EMPTY;
		ApplicationContext ctx = RequestContextUtils.getWebApplicationContext(request);
		//暂时默认为zh_CN，以后在开放其他语言
		//errorMsg = ctx.getMessage(code, args,request.getLocale());
		errorMsg = ctx.getMessage(code, args,Locale.SIMPLIFIED_CHINESE);
		return errorMsg;
	}

	/**
	 * 获取msg
	 * @param message
	 * @return
	 */
	public String getMsg(MessageDomain message) {
		String errorMsg = StringUtils.EMPTY;
		ApplicationContext ctx = RequestContextUtils.getWebApplicationContext(request);
		//暂时默认为zh_CN，以后在开放其他语言
		errorMsg = ctx.getMessage(String.valueOf(message.getCode()), message.getArgs() ,Locale.SIMPLIFIED_CHINESE);
		return errorMsg;
	}
}
